import axios from "../plugins/axios.min.js";
console.log(axios);
const service = axios.create({
    baseURL: baseUrl,
    headers: {
        post: {
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        },
    },
    //代理业务-签约列表接口响应时间超过原来的10000
    timeout: 100000,
});
//请求拦截
service.interceptors.request.use(
    (config) => {
        if (config.baseUrlNew) {
            config.baseURL = config.baseUrlNew;
        }
        //添加token
        config.headers.Token = sessionStorage.getItem("Token");
        config.headers.Platform = "vXBrUZ7rWE5D4P4ENnwR6GFvaG2pVgul";
        return config;
    },
    (error) => Promise.error(error)
);

//返回拦截
service.interceptors.response.use(
    (response) => {
        let code = response.data.code;
        if (code === 40100 || code === 40101 || code === 40102) {
            MessageBox.confirm(
                `用户${response.data.msg}，请重新登录`,
                "系统提示",
                {
                    confirmButtonText: "去登录",
                    cancelButtonText: "取消",
                    type: "warning",
                }
            )
                .then((res) => {
                    if (res === "confirm") {
                        store.dispatch("LogOut").then(() => {
                            location.href = "/index";
                        });
                    }
                })
                .catch(() => {
                    Message.info("取消登录");
                });
        } else if (code === 40600) {
            return Promise.resolve(response.data);
        } else if (code !== 200) {
            Notification.error({
                title: response.data.msg,
            });
            return Promise.reject(response.data);
        } else {
            return Promise.resolve(response.data);
        }
    },
    (error) => {
        let { message } = error;
        if (message == "Network Error") {
            message = "后端接口连接异常";
        } else if (message.includes("timeout")) {
            message = "系统接口请求超时";
        }
        // Message({
        //     message: message,
        //     type: 'error',
        //     duration: 5 * 1000
        // })
        let num = 5;
        MessageBox.alert(
            `<span style="font-size:16px;line-height:44px;">${message},请<strong style="color:red;font-size:18px;"> ${num} </strong>秒之后按键<strong style="color:red;font-size:18px;">Ctrl+F5</strong>，刷新页面后重试！</span>`,
            "系统提示",
            {
                dangerouslyUseHTMLString: true,
                center: true,
            }
        );

        return Promise.reject(error);
    }
);
export default service;
